<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
global $data, $admin;

class Reportes extends CI_Controller {

    public function __construct() {
        parent::__construct();
        global $admin, $data;
        # cargamos variables
        $admin = $this->session->userdata('admin');
        $admin['place'] = 'reportes';
        # verifica si pasó por sushitto
        if ($admin['ta_tipo_administrativo_id'] != 1) {
            redirect('sushitto');
        }
        $this->carga_estadisticas();
        $this->output->enable_profiler(FALSE);
    }

    public function crear() {
        global $admin, $data;
        $data['supervisores'] = $this->reportes_model->getListSupervisores();
        
        $data['sucursal'] = $this->evaluacion_model->lista_sucursales();
        $data['region'] = $this->reportes_model->lista_regiones();
        $data['lista_tipos_aspi'] = $this->evaluacion_model->tipos_aspi();
        
        
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_supervisor', $data);
        $this->load->view('reportes/panel_busqueda');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function carga_estadisticas() {
        global $data;
        $data['tPreg'] = $this->mining_model->total_preguntas();
        $data['tCuest'] = $this->mining_model->total_cuestionarios();
    }

    public function repa() {
        global $admin, $data;
        $suc = $this->input->post('sucursales');
        $fi = $this->input->post('fecha_inicial');
        $ff = $this->input->post('fecha_final');
        $data['repa1'] = $this->reportes_model->repa($suc, $fi, $ff, 1);
        $data['repa2'] = $this->reportes_model->repa($suc, $fi, $ff, 2);
        $data['repa3'] = $this->reportes_model->repa($suc, $fi, $ff, 3);
        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_supervisor', $data);
        $this->load->view('reportes/repa');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function repb() {
        global $admin, $data;

        $ta_tipo_aspi_id = $this->input->post('ta_tipo_aspi_id');
        $r_region_id = $this->input->post('r_region_id');

        $data['repb'] = $this->reportes_model->repb($ta_tipo_aspi_id, $r_region_id);

        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_supervisor', $data);
        $this->load->view('reportes/repb');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function repc() {
        global $admin, $data;
        $finicial = $this->input->post('finicial'); #fecha inicial
        $ffinal = $this->input->post('ffinal'); #fecha final
        $ta_tipo_aspi_id = $this->input->post('ta_tipo_aspi_id'); #tipo aspi
        $data['xUnidades'] = $this->reportes_model->xUnidades($finicial, $ffinal, $ta_tipo_aspi_id);
        $data['iBull'] = $this->reportes_model->iBull($finicial, $ffinal, $ta_tipo_aspi_id);
        $data['finicial'] = $finicial;
        $data['ffinal'] = $ffinal;
        $data['ta_tipo_aspi_id'] = $ta_tipo_aspi_id;

        $this->load->view('template/header', $admin);
        $this->load->view('template/open_content_container');
        $this->load->view('menu/menu_supervisor', $data);
        $this->load->view('reportes/repc');
        $this->load->view('template/close_content_container');
        $this->load->view('template/footer');
    }

    public function excel() {
        $finicial = $this->input->post('finicial'); #fecha inicial
        $ffinal = $this->input->post('ffinal'); #fecha final
        $ta_tipo_aspi_id = $this->input->post('ta_tipo_aspi_id'); #tipo aspi
        $xUnidades = $this->reportes_model->xUnidades($finicial, $ffinal, $ta_tipo_aspi_id);
        $iBull = $this->reportes_model->iBull($finicial, $ffinal, $ta_tipo_aspi_id);
        $this->load->library('excel');
        $this->excel->setActiveSheetIndex(0);
        //name the worksheet
        $this->excel->getActiveSheet()->setTitle('Reporte Cuatrimestre 1');
        //set cell A1 content with some text
        $this->excel->getActiveSheet()->setCellValue('A1', 'Matricula');
        $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('B1', 'Pregunta');
        $this->excel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('C1', 'PP');
        $this->excel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setSize(10);
        $INDEX = 'D';
        foreach ($xUnidades as $x) {
            $this->excel->getActiveSheet()->getStyle($INDEX . '1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $this->excel->getActiveSheet()->getStyle($INDEX . '1')->getFont()->setBold(true);
            $this->excel->getActiveSheet()->getStyle($INDEX . '1')->getFont()->setSize(10);
            $this->excel->getActiveSheet()->setCellValue($INDEX++ . '1', $x->sucursal);
        }
        $renglon = 2; # indice dle renglon donde empieza
        foreach ($iBull as $x) {
            $this->excel->getActiveSheet()->setCellValue('A' . $renglon, $x['matricula']);
            $this->excel->getActiveSheet()->setCellValue('B' . $renglon, $x['p_pregunta']);
            $this->excel->getActiveSheet()->setCellValue('C' . $renglon, $x['p_pp']);
            $i = 'D'; # indice de la columna
            foreach ($xUnidades as $t) {
                $this->excel->getActiveSheet()->setCellValue($i++ . $renglon, $x[str_replace(' ', '_', strtolower($t->sucursal))]);
            }
            $renglon++; # una vez terminado el barrido horizontal aumenta una posicion vertical
        }
        $filename = 'reporte.xlsx'; //save our workbook as this file name
        header('Content-Type: application/vnd.ms-excel'); //mime type
        header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
        header('Cache-Control: max-age=0'); //no cache
        //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
        //if you want to save it as .XLSX Excel 2007 format
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
        //force user to download the Excel file without writing it to server's HD
        $objWriter->save('php://output');
    }
    public function excelAudit() {
        $listaAuditada = $this->evaluacion_model->getListaSucAudit();
        $this->load->library('excel');
        $this->excel->setActiveSheetIndex(0);
        //name the worksheet
        $this->excel->getActiveSheet()->setTitle('Reporte Sucursales Auditadas');
        //set cell A1 content with some text
        $this->excel->getActiveSheet()->setCellValue('A1', 'Número de auditorias');
        $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('B1', 'Sucursal');
        $this->excel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('C1', 'Clave Sucursal');
        $this->excel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('D1', 'Auditor');
        $this->excel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('D1')->getFont()->setSize(10);

        $i = 2;
        foreach ($listaAuditada as $sa) {
        $this->excel->getActiveSheet()->setCellValue('A'. $i, $sa->nAuditorias);
        $this->excel->getActiveSheet()->setCellValue('B'. $i, $sa->sucursal);
        $this->excel->getActiveSheet()->setCellValue('C'. $i, $sa->claveSuc);
        $this->excel->getActiveSheet()->setCellValue('D'. $i, $sa->auditor);
        $i++;
        }

        $filename = 'reporte_sucursales_auditadas.xlsx'; //save our workbook as this file name
        header('Content-Type: application/vnd.ms-excel'); //mime type
        header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
        header('Cache-Control: max-age=0'); //no cache
        //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
        //if you want to save it as .XLSX Excel 2007 format
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
        //force user to download the Excel file without writing it to server's HD
        $objWriter->save('php://output');
    }
    
    public function excelAuditNo() {
        $listaNoAuditada = $this->evaluacion_model->getListaSucNoAudit();
        $this->load->library('excel');
        $this->excel->setActiveSheetIndex(0);
        //name the worksheet
        $this->excel->getActiveSheet()->setTitle('Reporte Sucursales no Auditadas');
        //set cell A1 content with some text
        $this->excel->getActiveSheet()->setCellValue('A1', 'Sucursal');
        $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('B1', 'Clave Sucursal');
        $this->excel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('B1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->setCellValue('C1', 'Auditor');
        $this->excel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('C1')->getFont()->setSize(10);

        $i = 2;
        foreach ($listaNoAuditada as $sna) {
        $this->excel->getActiveSheet()->setCellValue('A'. $i, $sna->sucursal);
        $this->excel->getActiveSheet()->setCellValue('B'. $i, $sna->claveSuc);
        //$this->excel->getActiveSheet()->setCellValue('C'. $i, $sna->auditor);
        $i++;
        }

        $filename = 'reporte_sucursales_no_auditadas.xlsx'; //save our workbook as this file name
        header('Content-Type: application/vnd.ms-excel'); //mime type
        header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
        header('Cache-Control: max-age=0'); //no cache
        //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
        //if you want to save it as .XLSX Excel 2007 format
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
        //force user to download the Excel file without writing it to server's HD
        $objWriter->save('php://output');
    }
    public function jsonlistAdmins(){        
        $a_administrativo_id = $this->input->post('a_administrativo_id');
        $listSucursales = $this->reportes_model->getListSucursalesXSuper($a_administrativo_id);
        $this->output->set_output(json_encode($listSucursales));
    }
}
